package com.vincent.service;

import com.vincent.common.enums.ResultMapKey;
import com.vincent.common.utils.PasswordKeyUtil;
import com.vincent.common.utils.RandomCharUtil;
import com.vincent.common.utils.SendEmailUtil;
import com.vincent.dao.AdminDao;
import com.vincent.model.Admin;
import com.vincent.vo.ForgetPwdVo;
import com.vincent.vo.LoginUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service("adminService")
public class AdminService {
    @Resource
    private SendEmailUtil sendEmailUtil;
    @Value("${fpwEmailSubject}")
    private String fpwEmailTitle;
    @Value("${fpwEmailContent}")
    private String fpwEmailContent;
    @Resource
    private AdminDao adminDao;


    /**
     * 验证管理员登录
     *
     * @param vo
     * @param sessionAuthcode
     * @return
     */
    public Map<String, Object> validateAdminLogin(LoginUserVo vo, Object sessionAuthcode) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (sessionAuthcode == null) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "验证码失效，请重新输入");
            return resultMap;
        }
        if (!vo.getAuthcode().equalsIgnoreCase(sessionAuthcode.toString())) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "验证码错误");
            return resultMap;
        }
        String userName = vo.getUserName();
        Admin admin = adminDao.findByUserName(userName);
        if (admin == null || !admin.getIsEnabled()) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "用户名或者密码错误");
            return resultMap;
        }
        String salt = admin.getSalt();
        if (StringUtils.isBlank(salt)) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "用户名或者密码错误");
            return resultMap;
        }
        String password = vo.getPassword();
        String saltPassword = PasswordKeyUtil.getSaltPassword(salt, password);
        admin = adminDao.selectByUserNameAndPwd(userName, saltPassword);
        if (null == admin) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "用户名或者密码错误");
            return resultMap;
        }
        admin.setUsername(userName);
        resultMap.put(ResultMapKey.SUCCESS.getCode(), admin);
        return resultMap;
    }

    /**
     * 找回密码 发送邮件新的密码
     *
     * @param vo
     * @param sessionAuthcode
     * @param isAdmin
     * @return
     */
    public Map<String, Object> validateForgetPwdSendEmail(ForgetPwdVo vo, Object sessionAuthcode, boolean isAdmin) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (sessionAuthcode == null) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "验证码失效，请重新输入");
            return resultMap;
        }
        if (!vo.getAuthcode().equalsIgnoreCase(sessionAuthcode.toString())) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "验证码错误");
            return resultMap;
        }
        String email = vo.getEmail();
        Admin admin = adminDao.findByUserName(vo.getUserName());
        if (admin == null || !email.equals(admin.getEmail()) || !admin.getIsEnabled()) {
            resultMap.put(ResultMapKey.ERROR.getCode(), "用户名和Email不匹配");
            return resultMap;
        }
        //修改密码
        String password = RandomCharUtil.getRandom(6);
        String salt = PasswordKeyUtil.getRandomKey();
        String saltPassword = PasswordKeyUtil.getSaltPassword(salt, password);
        admin.setSalt(salt);
        admin.setPassword(saltPassword);
        adminDao.update(admin);
        //发送邮件
        fpwEmailContent = fpwEmailContent.replace("{newpwd}", password);
        //发送邮件
        sendEmailUtil.simpleSend(email, fpwEmailTitle, fpwEmailContent);
        resultMap.put(ResultMapKey.SUCCESS.getCode(), "邮件发送成功");
        return resultMap;
    }


}
